package com.xy.p2p.service.impl;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xy.common.page.Pagination;
import com.xy.core.service.impl.BaseServiceImpl;
import com.xy.core.util.BeanUtil;
import com.xy.p2p.dao.TmemberDao;
import com.xy.p2p.dao.TmemberassetaccountDao;
import com.xy.p2p.dao.TmemberassetaccountdetailsDao;
import com.xy.p2p.dao.TrechargeDao;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Tmemberassetaccount;
import com.xy.p2p.model.Tmemberassetaccountdetails;
import com.xy.p2p.model.Trecharge;
import com.xy.p2p.service.TrechargeService;

@Service
@Transactional
public class TrechargeServiceImpl extends BaseServiceImpl<Trecharge> implements
		TrechargeService {
	
	//充值方向 -1：借（出金），1：贷（入金）
	private final int TMEMBERASSETACCOUNTDETAIL_BORROWING=1;
	//交易类型1:充值,其他见数据字典
	private final int TMEMBERASSETACCOUNTDETAIL_TRANTYPE=1;
	
	
	private TrechargeDao trechargeDao;

	@Autowired
	private TmemberassetaccountDao tmemberassetaccountDao;
	@Autowired
	private TmemberassetaccountdetailsDao tmemberassetaccountdetailsDao;
	@Autowired
	private TmemberDao tmemberDao; 
	
	public TrechargeServiceImpl(TrechargeDao paramAppRoleDao) {
		super(paramAppRoleDao);
		this.trechargeDao = paramAppRoleDao;
	}

	@Override
	public boolean checkExist(String paraName, String paraValue) {
		return trechargeDao.checkExist("Trecharge", paraName, paraValue);
	}

	@Override
	public List<Trecharge> getTrecharge(Map<String, Object> map) {
		return trechargeDao.getTrecharge(map);
	}
	@Override
	public Pagination getTrechargeByPage(Map<String, Object> map, int pageNo,
			int pageSize) {
		// TODO Auto-generated method stub
		return trechargeDao.getTrechargeByPage(map, pageNo, pageSize);
	}
	 
   @Override
	public Trecharge save(Trecharge trecharge) {
	//	trecharge.setModtime(new Date());
		if(trecharge.getFlowno()==null){
			//新增
        	return super.save(trecharge);
		}else{
			//更新
			Trecharge trecharge1=trechargeDao.get(trecharge.getFlowno());
//			Trecharge trecharge1=super.save(trecharge);
			try {
				BeanUtil.copyNotNullProperties(trecharge1, trecharge);
				trecharge1 = super.save(trecharge1);
				if(trecharge1.getStatus().equals("01"))	
				{
					//插入明细, 更新余额
					Map<String,Object> queryMap = new HashMap<String, Object>();
					queryMap.put("memberid", trecharge1.getMemberid());
					Tmember tmember = tmemberDao.getTmember(queryMap).get(0);
					queryMap.clear();
					queryMap.put("Tmember", tmember);
					Tmemberassetaccount tmemberassetaccount = tmemberassetaccountDao.getTmemberassetaccount(queryMap).get(0);
					//增加余额
					tmemberassetaccount.setBalance(tmemberassetaccount.getBalance()+Double.parseDouble(trecharge1.getAmount()));
					Tmemberassetaccountdetails tmemberassetaccountdetails = new Tmemberassetaccountdetails(tmember, Double.parseDouble(trecharge1.getAmount()),
							TMEMBERASSETACCOUNTDETAIL_BORROWING,TMEMBERASSETACCOUNTDETAIL_TRANTYPE , new Date(), "通联", null);
					tmemberassetaccountDao.save(tmemberassetaccount);
					tmemberassetaccountdetailsDao.save(tmemberassetaccountdetails);
//					return super.save(trecharge1);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}  
			return trecharge1;

		}
	}
}